home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libio / streambuf.cc < prev    next >
C/C++ Source or Header  |  1994-06-24  |  8KB  |  342 lines

  1. /* This is part of libio/iostream, providing -*- C++ -*- input/output.
  2. Copyright (C) 1991, 1992, 1993 Free Software Foundation
  3.  
  4. This file is part of the GNU IO Library.  This library is free
  5. software; you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU CC; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. As a special exception, if you link this library with files
  20. compiled with a GNU compiler to produce an executable, this does not cause
  21. the resulting executable to be covered by the GNU General Public License.
  22. This exception does not however invalidate any other reasons why
  23. the executable file might be covered by the GNU General Public License. */
  24.  
  25. /* Written by Per Bothner (bothner@cygnus.com). */
  26.  
  27. #define _STREAM_COMPAT
  28. #ifdef __GNUG__
  29. #pragma implementation
  30. #endif
  31. #include "iostreamP.h"
  32. #include <string.h>
  33. #include <stdarg.h>
  34. #include <errno.h>
  35. #ifndef errno
  36. extern int errno;
  37. #endif
  38.  
  39. void streambuf::_un_link() { _IO_un_link(this); }
  40.  
  41. void streambuf::_link_in() { _IO_link_in(this); }
  42.  
  43. int streambuf::switch_to_get_mode()
  44. { return _IO_switch_to_get_mode(this); }
  45.  
  46. void streambuf::free_backup_area()
  47. { _IO_free_backup_area(this); }
  48.  
  49. #if 0
  50. int streambuf::switch_to_put_mode()
  51. { return _IO_:switch_to_put_mode(this); }
  52. #endif
  53.  
  54. int __overflow(streambuf* sb, int c)
  55. {
  56.     return sb->overflow(c);
  57. }
  58.  
  59. int streambuf::underflow()
  60. { return EOF; }
  61.  
  62. int streambuf::overflow(int c /* = EOF */)
  63. { return EOF; }
  64.  
  65. streamsize streambuf::xsputn(register const char* s, streamsize n)
  66. { return _IO_default_xsputn(this, s, n); }
  67.  
  68. streamsize streambuf::xsgetn(char* s, streamsize n)
  69. { return _IO_default_xsgetn(this, s, n); }
  70.  
  71. int streambuf::ignore(int n)
  72. {
  73.     register int more = n;
  74.     for (;;) {
  75.     int count = _IO_read_end - _IO_read_ptr; // Data available.
  76.     if (count > 0) {
  77.         if (count > more)
  78.         count = more;
  79.         _IO_read_ptr += count;
  80.         more -= count;
  81.     }
  82.     if (more == 0 || __underflow(this) == EOF)
  83.         break;
  84.     }
  85.     return n - more;
  86. }
  87.  
  88. int streambuf::sync()
  89. {
  90.     if (pptr() == pbase())
  91.     return 0;
  92.     return EOF;
  93. }
  94.  
  95. int streambuf::pbackfail(int c)
  96. {
  97.   return _IO_default_pbackfail(this, c);
  98. }
  99.  
  100. streambuf* streambuf::setbuf(char* p, int len)
  101. {
  102.     if (sync() == EOF)
  103.     return NULL;
  104.     if (p == NULL || len == 0) {
  105.     unbuffered(1);
  106.     setb(_shortbuf, _shortbuf+1, 0);
  107.     }
  108.     else {
  109.     unbuffered(0);
  110.     setb(p, p+len, 0);
  111.     }
  112.     setp(0, 0);
  113.     setg(0, 0, 0);
  114.     return this;
  115. }
  116.  
  117. streampos streambuf::seekpos(streampos pos, int mode)
  118. {
  119.     return seekoff(pos, ios::beg, mode);
  120. }
  121.  
  122. streampos streambuf::sseekpos(streampos pos, int mode)
  123. {
  124.   return _IO_seekpos (this, pos, convert_to_seekflags (0, mode));
  125. }
  126.  
  127. void streambuf::setb(char* b, char* eb, int a)
  128. { _IO_setb(this, b, eb, a); }
  129.  
  130. int streambuf::doallocate() { return _IO_default_doallocate(this); }
  131.  
  132. void streambuf::doallocbuf() { _IO_doallocbuf(this); }
  133.  
  134. /* The following are jump table entries that just call the virtual method */
  135.  
  136. static int _IO_sb_overflow(_IO_FILE *fp, int c)
  137. { return ((streambuf*)fp)->overflow(c); }
  138. static int _IO_sb_underflow(_IO_FILE *fp)
  139. { return ((streambuf*)fp)->underflow(); }
  140. static _IO_size_t _IO_sb_xsputn(_IO_FILE *fp, const void *s, _IO_size_t n)
  141. { return ((streambuf*)fp)->xsputn((const char*)s, n); }
  142. static _IO_size_t _IO_sb_xsgetn(_IO_FILE *fp, void *s, _IO_size_t n)
  143. { return ((streambuf*)fp)->xsgetn((char*)s, n); }
  144. static int _IO_sb_close(_IO_FILE *fp)
  145. { return ((streambuf*)fp)->sys_close(); }
  146. static int _IO_sb_stat(_IO_FILE *fp, void *b)
  147. { return ((streambuf*)fp)->sys_stat(b); }
  148. static int _IO_sb_doallocate(_IO_FILE *fp)
  149. { return ((streambuf*)fp)->doallocate(); }
  150.  
  151. static _IO_pos_t _IO_sb_seekoff(_IO_FILE *fp, _IO_off_t pos, _IO_seekflags m)
  152. {
  153.   int mode = ((m & _IO_seek_not_in) ? 0 : ios::in)
  154.     + ((m & _IO_seek_not_out) ? 0 : ios::out);
  155.   return ((streambuf*)fp)->seekoff(pos, (_seek_dir)((int)m & 3), mode);
  156. }
  157.  
  158. static _IO_pos_t _IO_sb_seekpos(_IO_FILE *fp, _IO_pos_t pos, _IO_seekflags m)
  159. {
  160.   int mode = ((m & _IO_seek_not_in) ? 0 : ios::in)
  161.     + ((m & _IO_seek_not_out) ? 0 : ios::out);
  162.   return ((streambuf*)fp)->seekpos(pos, mode);
  163. }
  164.  
  165. static int _IO_sb_pbackfail(_IO_FILE *fp, int ch)
  166. { return ((streambuf*)fp)->pbackfail(ch); }
  167. static void _IO_sb_finish(_IO_FILE *fp)
  168. { ((streambuf*)fp)->~streambuf(); }
  169. static _IO_ssize_t _IO_sb_read(_IO_FILE *fp, void *buf, _IO_ssize_t n)
  170. { return ((streambuf*)fp)->sys_read((char*)buf, n); }
  171. static _IO_ssize_t _IO_sb_write(_IO_FILE *fp, const void *buf, _IO_ssize_t n)
  172. { return ((streambuf*)fp)->sys_write((const char*)buf, n); }
  173. static int _IO_sb_sync(_IO_FILE *fp)
  174. { return ((streambuf*)fp)->sync(); }
  175. static _IO_pos_t _IO_sb_seek(_IO_FILE *fp, _IO_off_t off, int dir)
  176. { return ((streambuf*)fp)->sys_seek(off, (_seek_dir)dir); }
  177. static int _IO_sb_setbuf(_IO_FILE *fp, char *buf, _IO_ssize_t n)
  178. { return ((streambuf*)fp)->setbuf(buf, n) == NULL ? EOF : 0; }
  179.  
  180. /* This callbacks in this jumptable just call the corresponding
  181.    virtual function, so that C functions can access (potentially user-defined)
  182.    streambuf-derived objects.
  183.    Contrast the builtinbuf class, which does the converse:  Allow
  184.    C++ virtual calls to to be used on _IO_FILE objects that are builtin
  185.    (or defined by C code). */
  186.  
  187.  
  188. struct _IO_jump_t _IO_streambuf_jumps = {
  189.   _IO_sb_overflow,
  190.   _IO_sb_underflow,
  191.   _IO_sb_xsputn,
  192.   _IO_sb_xsgetn,
  193.   _IO_sb_read,
  194.   _IO_sb_write,
  195.   _IO_sb_doallocate,
  196.   _IO_sb_pbackfail,
  197.   _IO_sb_setbuf,
  198.   _IO_sb_sync,
  199.   _IO_sb_finish,
  200.   _IO_sb_close,
  201.   _IO_sb_stat,
  202.   _IO_sb_seek,
  203.   _IO_sb_seekoff,
  204.   _IO_sb_seekpos,
  205.   _IO_default_uflow
  206. };
  207.  
  208. streambuf::streambuf(int flags)
  209. {
  210.   _IO_init(this, flags);
  211.   _jumps = &_IO_streambuf_jumps;
  212. }
  213.  
  214. streambuf::~streambuf() { _IO_default_finish(this); }
  215.  
  216. streampos
  217. streambuf::seekoff(streamoff, _seek_dir, int mode /*=ios::in|ios::out*/)
  218. {
  219.     return EOF;
  220. }
  221.  
  222. streampos
  223. streambuf::sseekoff(streamoff o , _seek_dir d, int m /*=ios::in|ios::out*/)
  224. {
  225.   return _IO_seekoff (this, o, convert_to_seekflags (d, m));
  226. }
  227.  
  228. int streambuf::sputbackc(char c)
  229. {
  230.   return _IO_sputbackc(this, c);
  231. }
  232.  
  233. int streambuf::sungetc()
  234. {
  235.   return _IO_sungetc(this);
  236. }
  237.  
  238. #if 0 /* Work in progress */
  239. void streambuf::collumn(int c)
  240. {
  241.     if (c == -1)
  242.     _collumn = -1;
  243.     else
  244.     _collumn = c - (_IO_write_ptr - _IO_write_base);
  245. }
  246. #endif
  247.  
  248.  
  249. int streambuf::get_column()
  250. {
  251.     if (_cur_column) 
  252.     return _IO_adjust_column(_cur_column - 1, pbase(), pptr() - pbase());
  253.     return -1;
  254. }
  255.  
  256. int streambuf::set_column(int i)
  257. {
  258.     _cur_column = i+1;
  259.     return 0;
  260. }
  261.  
  262. int streambuf::flush_all() { return _IO_flush_all (); }
  263.  
  264. void streambuf::flush_all_linebuffered()
  265. { _IO_flush_all_linebuffered(); }
  266.  
  267. int streambuf::sys_stat(void *)
  268. {
  269. #ifdef EIO
  270.   errno = EIO;
  271. #endif
  272.   return -1;
  273. }
  274.  
  275. streamsize streambuf::sys_read(char* buf, streamsize size)
  276. {
  277.   return 0;
  278. }
  279.  
  280. streamsize streambuf::sys_write(const char* buf, streamsize size)
  281. {
  282.   return 0;
  283. }
  284.  
  285. streampos streambuf::sys_seek(streamoff, _seek_dir)
  286. {
  287.   return EOF;
  288. }
  289.  
  290. int streambuf::sys_close() { return 0; /* Suceess; do nothing */ }
  291.  
  292. streammarker::streammarker(streambuf *sb)
  293. {
  294.   _IO_init_marker(this, sb);
  295. }
  296.  
  297. streammarker::~streammarker()
  298. {
  299.   _IO_remove_marker(this);
  300. }
  301.  
  302. #define BAD_DELTA EOF
  303.  
  304. int streammarker::delta(streammarker& other_mark)
  305. {
  306.   return _IO_marker_difference(this, &other_mark);
  307. }
  308.  
  309. int streammarker::delta()
  310. {
  311.   return _IO_marker_delta(this);
  312. }
  313.  
  314. int streambuf::seekmark(streammarker& mark, int delta /* = 0 */)
  315. {
  316.   return _IO_seekmark(this, &mark, delta);
  317. }
  318.  
  319. void streambuf::unsave_markers()
  320. {
  321.   _IO_unsave_markers(this);
  322. }
  323.  
  324. int ios::readable() { return !(rdbuf()->_flags & _IO_NO_READS); }
  325. int ios::writable() { return !(rdbuf()->_flags & _IO_NO_WRITES); }
  326. int ios::is_open() { return rdbuf()
  327.              && (rdbuf()->_flags & _IO_NO_READS+_IO_NO_WRITES)
  328.                  != _IO_NO_READS+_IO_NO_WRITES; }
  329.  
  330. #if defined(linux)
  331. #define IO_CLEANUP
  332. #endif
  333.  
  334. #ifdef IO_CLEANUP
  335.   IO_CLEANUP
  336. #else
  337. struct __io_defs {
  338.     ~__io_defs() { _IO_cleanup (); }
  339. };   
  340. __io_defs io_defs__;
  341. #endif
  342.